package org.jsola.hr.service.excel;

import com.alibaba.excel.context.AnalysisContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.jsola.common.BeanKit;
import org.jsola.exception.ParamException;
import org.jsola.hr.dto.excel.SalaryBillImportDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.service.ISalaryBillService;
import org.jsola.hr.vo.SalaryBillImportExcelVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhr
 */
@Slf4j
public class SalaryBillImportListener extends BaseAnalysisEventListener<SalaryBillImportDTO> {


    /**
     * List最大盛装数据条数
     */
    private static final Integer MAX_COUNT = 2000;

    /**
     * 公司ID
     */
    protected String companyId;

    /**
     * 年份
     */
    protected String year;

    /**
     * 月份
     */
    protected String month;

    /**
     * 工资表名称
     */
    protected String tableName;

    /**
     * 导入结果VO
     */
    protected SalaryBillImportExcelVO salaryBillImportExcelVO;

    private Map<Integer, String> headMap;

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {

        log.info("{}", headMap);
        this.headMap = headMap;

        // 验证EXCEL表头对应名称
        List<String> stringList = new ArrayList<>();
        stringList.add("员工姓名");
        stringList.add("工号");
        stringList.add("部门名称");
        stringList.add("岗位名称");
        stringList.add("手机号");
        //stringList.add("基本工资");
        stringList.add("应发工资");
        stringList.add("个人社保");
        stringList.add("个人公积金");
        stringList.add("个人所得税");
        stringList.add("实发工资");
        stringList.add("考勤扣款");

        if (!headMap.containsValue("员工姓名")
                || !headMap.containsValue("手机号") || !headMap.containsValue("应发工资") || !headMap.containsValue("实发工资")) {
            throw new ParamException("导入的模板不正确,必填项未填,请检查");
        }
    }

    @Override
    public void invoke(SalaryBillImportDTO salaryBillImportDTO, AnalysisContext context) {
        // 当前行号
        int row = context.readRowHolder().getRowIndex();
        salaryBillImportDTO.setId(row + "");
        dataList.add(salaryBillImportDTO);
        if (dataList.size() >= MAX_COUNT) {
            // 入库
            saveData();
            // 存储完成清理 list
            dataList.clear();
        }
    }

    /**
     * 入库
     */
    @Override
    public void saveData() {
        log.info("{}条数据，开始存储数据库！", dataList.size());
        if (dataList.size() == 0) {
            log.warn("没有数据需要入库");
            throw new ParamException("没有数据需要入库，请检查EXCEL文件内容");
        }
        HrException.notNull(tokenUser, "没有设置当前登录用户");

        ISalaryBillService salaryBillService = BeanKit.getBean(ISalaryBillService.class);
        salaryBillImportExcelVO = salaryBillService.saveExcelDataList(dataList, tokenUser, companyId, year, month, tableName, headMap);
        log.info("存储数据库成功！");
    }


    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public SalaryBillImportExcelVO getSalaryBillImportExcelVO() {
        return salaryBillImportExcelVO;
    }

    public void setSalaryBillImportExcelVO(SalaryBillImportExcelVO salaryBillImportExcelVO) {
        this.salaryBillImportExcelVO = salaryBillImportExcelVO;
    }


}
